package com.fancy.learncenter.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.audiofx.Equalizer;

import com.fancy.learncenter.bean.AudioBean;

import java.util.ArrayList;

/**
 * Created by Hyy on 2017/1/6.
 */

public class RecorderAudioDBManager {

    private SQLiteDatabase db;
    private static String TABLENAME ;
    private static final String DBNAME = "learn.db";// 数据库名称
    private static RecorderAudioDBManager mRecordDB;

    private final String tabel = "CREATE table IF NOT EXISTS _" + TABLENAME
            + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,playLink TEXT,customerId long,coverImg TEXT,name TEXT)";

    public RecorderAudioDBManager(Context context) {
        db = context.openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
    }

    public static RecorderAudioDBManager getInstance(Context context, String tableName) {
        if (mRecordDB == null) {
            mRecordDB = new RecorderAudioDBManager(context);
        }

        TABLENAME = tableName;
        return mRecordDB;
    }


    public void addAudioBook(AudioBean audioBean) {
        db.execSQL(tabel);
        db.execSQL("insert into _" + TABLENAME + " (customerId,playLink,coverImg,name)" + " values(?,?,?,?)",
                new Object[]{audioBean.getCustomerId(), audioBean.getPlayLink(), audioBean.getCoverImg(), audioBean.getName()});
        close();
    }

    public void deleteAudioBook(String playLink) {
        db.execSQL(tabel);
        String DELETE_DATA = "DELETE FROM _" + TABLENAME + " WHERE playLink=" + "'" + playLink + "'";
        db.execSQL(DELETE_DATA);
        close();
    }

    public void updateAudioBook(AudioBean audioBean) {
//        ContentValues cv = new ContentValues();
//        cv.put("title", audioBean.getTitle());
//        String where = "urlPath" + "=" + "'" + audioBean.getUrlPath() + "'";
//        db.update("_" + TABLENAME, cv, where, null);
//        close();
    }

    public ArrayList<AudioBean> getAudioBookList(long customId) {
        ArrayList<AudioBean> list = new ArrayList<>();

        try {
            db.execSQL(tabel);
            Cursor c = db.rawQuery("SELECT * from _" + TABLENAME
                    + " ORDER BY _id DESC", null);
            while (c.moveToNext()) {
                AudioBean entity = new AudioBean();
                entity.setCoverImg(c.getString(c.getColumnIndex("coverImg")));

                long customerId = c.getLong(c.getColumnIndex("customerId"));
                entity.setCustomerId(customerId);
                entity.setPlayLink(c.getString(c.getColumnIndex("playLink")));
                entity.setName(c.getString(c.getColumnIndex("name")));

                if (customId == customerId) {
                    list.add(entity);
                }
            }
            c.close();
        }catch (Exception e){

        }

        return list;
    }

    public void close() {
        if (db != null) {
            db.close();
        }
//
    }
}
